package com.ios.controller;

import com.ios.dto.FMExecution;
import com.ios.entity.FM;
import com.ios.enums.FMStateEnum;
import com.ios.service.impl.FMServiceImpl;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/fm",method = {RequestMethod.POST})
public class FMController {
    @Autowired
    private FMServiceImpl fmService;

    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> addFM(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        FM fm = new FM();
        String title = HttpServletRequestUtil.getString(request,"title");
        String author = HttpServletRequestUtil.getString(request,"author");
        fm.setTitle(title);
        fm.setAuthor(author);
        //创建一个多分解的容器
        MultipartResolver MultipartResolver
                = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断是否有文件上传
        if (MultipartResolver.isMultipart(request)){
            //将request转换成多分解请求
            MultipartHttpServletRequest multipartHttpServletRequest
                    =MultipartResolver.resolveMultipart(request);
            MultipartFile fmfile = multipartHttpServletRequest.getFile("fmfile");
            /*fm添加*/
            if (fm!=null && fmfile!=null){
                try{
                    FMExecution fe = fmService.AddFM(fm,fmfile.getInputStream(),fmfile.getOriginalFilename());
                    if (fe.getState()==FMStateEnum.SUCCESS_ADD.getState()){
                        modelMap.put("success",true);
                        modelMap.put("FMInfo",fe.getFm());
                    }else {
                        modelMap.put("success",false);
                        modelMap.put("errMsg",fe.getStateInfo());
                    }
                }catch (IOException e){
                    modelMap.put("success",false);
                    modelMap.put("errMsg",e.getMessage());
                }
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg","请填写完整信息");
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg2","文件不能为空");
        }
        return modelMap;
    }

    @RequestMapping(value = "/showlist")
    @ResponseBody
    public Map<String,Object> showFMList(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try{
            FMExecution fm = fmService.ShowList();
            if (fm.getState()== FMStateEnum.SUCCESS_SHOW.getState()){
                modelMap.put("success",true);
                modelMap.put("FMList",fm.getFmList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",fm.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }
}
